#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>
#include <GL/glu.h>
#include <iostream>

#include "include/e_object.h"
#include "include/e_timer.h"
#include "include/e_rts_camera.h"
#include "include/e_textures.h"
#include "include/e_nodes.h"

using namespace std;

//Screen attributes
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define ASPECT_RATIO SCREEN_WIDTH/SCREEN_HEIGHT

#define SCREEN_BPP 32 //pixel depth

//The frame rate
#define FRAMES_PER_SECOND 60

bool init_GL()
{
    //Set clear color
    glClearColor( 0, 0, 0, 0 ); //black


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f, 1.333f, 0.2f, 255.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable( GL_TEXTURE_2D );

    //If there was any errors
    if( glGetError() != GL_NO_ERROR ){
        return false;
    }

    //If everything initialized
    return true;
}

bool init()
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ){
        return false;
    }

    //Create Window
    if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL ){
        cerr << "video mode setting failed: " << SDL_GetError() << endl;
        return false;
    }

    //Initialize OpenGL
    if( init_GL() == false ){
        return false;
    }

    //Set caption
    SDL_WM_SetCaption( "OpenGL Test", NULL );

    return true;
}


//protos
void RenderFrame (void);

int main( int argc, char *argv[] ){
    SDL_Event event;
    bool quit = false;

    if( init() == false ){
        SDL_Quit();
        return 1;
    }

    e_timer fps;
    e_rts_camera camera;

    e_textures atext("atext.png");
    atext.activate();

    e_cube acube;
    acube.anchor.x = 0; acube.anchor.y = 0; acube.anchor.z = -4.0f;
    acube.dimension = 3;
    e_nodes anode(NULL, acube);

	//Main Loop
	while( quit == false ){

        //While there are events to handle
		while( SDL_PollEvent( &event ) ){

            //Handle key presses
            camera.handle_input(&event);

			if( event.type == SDL_QUIT ){
                quit = true;
            }
		}

        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ){
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }

        fps.start();
        camera.update();

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBegin(GL_QUADS);

        glColor3f(0.0f, 1.0f, 1.0f);
        glTexCoord2f(0.0f,0.0f);
        glVertex3f(-5.0f, -5.0f, -4.0f);

        glColor3f(0.0f, 0.0f, 1.0f);
        glTexCoord2f(1.0f,0.0f);
        glVertex3f( 5.0f, -5.0f, -4.0f);

        glColor3f(0.0f, 1.0f, 0.0f);
        glTexCoord2f(1.0f,1.0f);
        glVertex3f( 5.0f,  5.0f, -4.0f);

        glColor3f(1.0f, 0.0f, 0.0f);
        glTexCoord2f(0.0f,1.0f);
        glVertex3f(-5.0f,  5.0f, -4.0f);

        glEnd();

        glColor3f(1.0f, 1.0f, 0.0f);
        anode.show();


        glLoadIdentity();

	    SDL_GL_SwapBuffers();

	}

    atext.~e_textures();

	SDL_Quit();

	return 0;
}

void RenderFrame (void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glTranslatef( 0, 0, 0 );
    glBegin(GL_QUADS);

    //glColor3f(0.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0f,0.0f);
    glVertex3f(-5.0f, -5.0f, -4.0f);

    //glColor3f(0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f,0.0f);
    glVertex3f( 5.0f, -5.0f, -4.0f);

    //glColor3f(0.0f, 1.0f, 0.0f);
    glTexCoord2f(1.0f,1.0f);
    glVertex3f( 5.0f,  5.0f, -4.0f);

    //glColor3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f,1.0f);
    glVertex3f(-5.0f,  5.0f, -4.0f);

    glEnd();

    glLoadIdentity();
}


